<template>
	<!-- 运营系统-小程序管理 -->
	<d2-container class="d-flex flex-column playlet—applet">
		<template v-if="$hasPermission('playlet:applet:index')">
			<div class="d-flex j-sb" slot="header">
				<el-form class="d-flex b-s mt-2 mb-10" :inline="true" size="small" :model="dataForm"
					@keyup.enter.native="getTableList(true)" @submit.native.prevent>
					<el-form-item label="小程序名称">
						<el-input v-model="dataForm.keyword" placeholder="小程序名称" clearable />
					</el-form-item>
					<el-form-item>
						<el-button icon="el-icon-search" type="primary"
							@click="getTableList(true)">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="success" icon="el-icon-circle-plus-outline" v-show="$hasPermission('playlet:applet:create')"
							@click="addOrUpdateData()">新增</el-button>
					</el-form-item>
				</el-form>
				<div class="d-flex">
					<vxe-toolbar ref="toolbarRef" custom :refresh="{ queryMethod: getTableList,code:'query' }"></vxe-toolbar>
				</div>
			</div>
			<div class="d-flex h-100 b-s flex-column">
				<div class="d-flex flex-1">
					<div class="overflow">
						<vxe-table ref="appletTable" key="appletTable" border height="100%" resizable size="medium"
							:loading="tableLoading" :loading-config="{icon: 'vxe-icon-indicator roll', text: '正在拼命加载中...'}"
							:data="dataList">
							<vxe-column width="100" field="id" title="ID" sortable align="center"></vxe-column>
							<vxe-column width="150" field="cover" title="小程序图标" align="center">
								<template v-slot="{ row }">
									<img class="pointer" width="50" height="auto" :src="row.cover" style="border-radius: 5px;"
										@click.stop="previewPicFn(row)">
								</template>
							</vxe-column>
							<vxe-column width="200" field="name" title="小程序名称" align="center"></vxe-column>
							<vxe-column width="260" field="type_name" title="类型" align="center"></vxe-column>
							<vxe-column width="260" field="app_id" title="appId" align="center"></vxe-column>
							<vxe-column width="150" field="coin_name" title="金币名称" align="center"></vxe-column>
							<vxe-column width="150" field="status" title="状态" align="center">
								<template v-slot="{ row }">
									<div>{{row.status==0?'禁用':'正常'}}</div>
								</template>
							</vxe-column>
							<vxe-column width="160" field="created_at" title="创建时间" sortable cell-type="string" align="center"></vxe-column>
							<vxe-column width="160" field="updated_at" title="修改时间" sortable cell-type="string" align="center"></vxe-column>
							<vxe-column v-if="!tableLoading||(dataList.length&&$hasPermissionNum(['playlet:applet:bindPlaylet','playlet:applet:update','playlet:applet:delete','playlet:applet:bindPlayletDelivery']))" title="操作" fixed="right" :width="($hasPermissionNum(['playlet:applet:bindPlaylet','playlet:applet:update','playlet:applet:delete','playlet:applet:bindPlayletDelivery']))*100" align="center" cell-class-name="py-10">
								<template v-slot="{ row }">
									<el-button size="small" round icon = "el-icon-position"
									v-if="$hasPermission('playlet:applet:bindPlaylet')"
									@click="bindPlayletData(row,1)"  type="primary">绑定短剧</el-button>

									<el-button size="small" round icon = "el-icon-position"
									v-if="$hasPermission('playlet:applet:bindPlayletDelivery')"
									@click="bindPlayletData(row,2)"  type="primary">绑定投放短剧</el-button>

									<el-button size="small" round icon="el-icon-edit"
									v-if="$hasPermission('playlet:applet:update')"
									@click="addOrUpdateData(row)" type="primary">修改</el-button>
									<el-button size="small" round icon="el-icon-delete"
									v-if="$hasPermission('playlet:applet:delete')"
									@click = "deleteHandleSetter(row)" type="danger">删除</el-button>
								</template>
							</vxe-column>
							<template #empty>
								<div class="d-flex flex-row a-center j-center font-md">
									<div class="d-flex">
										暂时没有数据~
									</div>
								</div>
							</template>
						</vxe-table>
					</div>
				</div>
			</div>
			<!-- 分页 -->
			<div class="d-flex" slot="footer">
				<vxe-pager background size="medium" align="right" :border="false" :pageSizes="[20,50,100,150,200]"
					:current-page="dataForm.page" :page-size="dataForm.pageSize" :total="total"  :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump', 'Total']"
					@page-change="handlePageChange">
				</vxe-pager>
			</div>
			<!-- 新增/修改弹窗 -->
			<applet-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateDialog" :appletTypeList="appletTypeList"  @refreshDataList="getTableList"></applet-add-or-update>
			<!-- 绑定小程序弹窗 -->
			<applet-bind v-if="bindVisible" ref="bindDialog" @refreshDataList="getTableList"></applet-bind>
			<!-- 图片预览弹窗 -->
			<pic-preview v-if="picVisible" ref="picDialog"></pic-preview>
		</template>
		<template v-else>
			<div class="d-flex a-center j-center h-100 b-s flex-column">
				<img class="d-flex b-s mt-2 mb-10" src="../../../public/image/download.png" alt="没有访问权限" style="width: 300px;">
				<div class="d-flex">抱歉，您没有访问权限</div>
			</div>
		</template>
	</d2-container>
</template>

<script>
	let that;
	import mixinViewModule from '@/mixins/view-module'
import { set } from 'lodash';
	import AppletAddOrUpdate from './applet-add-or-update'
	import AppletBind from './applet-bind'
	import PicPreview from './picPreview'
	export default {
		mixins: [mixinViewModule],
		components: {
			AppletAddOrUpdate,
			AppletBind,
			PicPreview
		},
		data() {
			return {
				
				tableToolbar: {
					perfect: true,
					refresh: true,
					zoom: true,
					custom: true,
					slots: {
					  buttons: 'toolbar_buttons'
					}
				},
				tableLoading:false,
				total:0,
				picVisible: false,
				bindVisible: false,
				//增改
				uploadFileVisible: false,
				addOrUpdateVisible: false,
				roleVisible:false,
				dataList:[],
				dataForm: {
					page:1,
					pageSize:20,
					keyword: ''
				},
			}
		},
		created() {
			console.log(137,this.$hasPermission('playlet:applet:index'))
			if(this.$hasPermission('playlet:applet:index')){
				this.getTableList();
				console.log(139139)
				this.getAppletType()
			}
		},
		mounted(){
			that = this;
			console.log()
		},
		methods: {
			//预览图片
			previewPicFn(row) {
				this.picVisible = true;
				this.$nextTick(() => {
					this.$refs.picDialog.update(row.cover, row.name+' 小程序图标');
				})
			},
			async getTableList(init=false){
				if(init&&typeof init =='boolean'){
					this.dataForm.page = 1;
				}
				this.tableLoading = true;
				const tableRes = await this.$axios.get('/api/playlet/applet?'+this.$qs.stringify(this.dataForm))
				this.tableLoading = false;
				this.dataList = tableRes.data;
				this.total = tableRes.total;
			},
			async cellClickEvent({row, rowIndex,columnIndex}){
				if(columnIndex==1){
					// console.log(row);
					// this.uploadFileVisible = true;
					// if (typeof row == 'object' && row!=null) {
					// 	const id = row.id;
					// 	const userRes = await this.$axios.get(`/api/playlet/applet/update/${id}`);
					// 	this.$nextTick(() => {
					// 		this.$refs.uploadFileDialog.userId = id;
					// 		this.$refs.uploadFileDialog.title = '上传小程序图标';
					// 		this.$refs.uploadFileDialog.update(userRes);
					// 	})
					// } else {
					// 	this.$nextTick(() => {
					// 		this.$refs.uploadFileDialog.userId = null;
					// 		this.$refs.uploadFileDialog.title = '上传小程序图标';
					// 		this.$refs.uploadFileDialog.init()
					// 	})
					// }
				}
			},
			//绑定小程序弹窗
			async bindPlayletData(row,type) {
				console.log(row,type)
				this.bindVisible = true;
				if (typeof row == 'object' && row!=null) {
					// this.curName = row.real_name;
					const id = row.id;
					const userRes = await this.$axios.get(type==1?`/api/playlet/applet/bindPlaylet/${id}`:`/api/playlet/applet/bindPlayletDelivery/${id}`);
					this.$nextTick(() => {
						this.$refs.bindDialog.appletId = id;
						this.$refs.bindDialog.diytype = type;
						this.$refs.bindDialog.update(userRes);
					})
				}
			},
			//新增/修改弹窗
			async addOrUpdateData(row) {
				console.log(row)
				this.addOrUpdateVisible = true;
				if (typeof row == 'object' && row!=null) {
					// this.curName = row.real_name;
					const id = row.id;
					const userRes = await this.$axios.get(`/api/playlet/applet/update/${id}`);
					this.$nextTick(() => {
						this.$refs.addOrUpdateDialog.ischange = true,
						this.$refs.addOrUpdateDialog.appletId = id;
						this.$refs.addOrUpdateDialog.update(userRes);
					})
				} else {
					this.$nextTick(() => {
						this.$refs.addOrUpdateDialog.ischange = false,
						this.$refs.addOrUpdateDialog.appletId = null;
						this.$refs.addOrUpdateDialog.dataForm = {
							name: '', //名称
							app_id: '',
							app_secret: '',
							cover: '',
							user_default_headimg: '',
							coin_name: '',
							token: '',
							static_base_url: '',
							type: null,
							status: 1, //状态
						}
						this.$refs.addOrUpdateDialog.init()
					})
				}
			},
			async getAppletType(){
				const userRes = await this.$axios.get(`/api/common/appletType`);
				console.log(229,userRes)
				this.appletTypeList = userRes
			},
			// 删除
			deleteHandleSetter(row) {
				this.$confirm(this.$t('public.prompt.info', {
					handle: this.$t('views.public.delete')
				}), this.$t('public.prompt.title'), {
					confirmButtonText: this.$t('views.public.confirm'),
					cancelButtonText: this.$t('views.public.cancel'),
					type: 'warning'
				}).then(() => {
					this.$axios.delete(`/api/playlet/applet/delete/${row.id}`,{}
					).then(res => {
						this.$message({
							message: this.$t('views.public.success'),
							type: 'success',
							duration: 500,
							onClose: () => {
								this.getTableList()
							}
						})
					}).catch(() => {})
				}).catch(() => {})
			}
		}
	}
</script>

<style>

</style>